dojo.provide("uws.drawing.ComponentFocusManager");

dojo.declare('uws.drawing.ComponentFocusManager',null,{
	/*
		Provides visual feedback (if supported) when a component is selected or deselected. 
		It also responds to Move to Front or Move to Back commands and broadcasts the properties 
		of the selected component.
	*/
	
	component:null,
	moving:null,
	
	//handlers
	tl_down:null,
	tl_move:null,
	tl_up:null,
	
	tr_down:null,
	tr_move:null,
	tr_up:null,
	
	bl_down:null,
	bl_move:null,
	bl_up:null,
	
	br_down:null,
	br_move:null,
	br_up:null,
	
	constructor:function(){},
	focus:function(component){
		if(this.component==null && component!=null && component!=undefined){
			this.component = component;
			//surfaceManager.requestFocus(surface);
			if(this.component.boundingBox != null){
				this.component.boundingBox.setStroke("black");
			}
			
			if(this.component.tl_res != null){
				try{
					this.component.resize_shape_group.moveToFront()
					this.component.tl_res.setFill("green").moveToFront();
					this.component.tr_res.setFill("green").moveToFront();
					this.component.bl_res.setFill("green").moveToFront();
					this.component.br_res.setFill("green").moveToFront();
					
					this.component.tl_res.connect("onmousedown",null,function(e){
						componentFocusManager.moving = true;
						componentFocusManager.component.shape_moveable.destroy();
						console.debug("down");
					});
					this.component.tl_res.connect("onmousemove",null,function(e){
						if(componentFocusManager.moving){
							try{
								componentFocusManager.component.actual_shape_group.applyTransform(dojox.gfx.matrix.scale(2,2));
							}catch(e){
								console.dir(e);
							}
							console.debug("mouse moving to "+e.layerX+", "+e.layerY);
						}
					});
					this.component.tl_res.connect("onmouseout",null,function(e){
						if(componentFocusManager.moving){
							componentFocusManager.moving = false;
							componentFocusManager.component.shape_moveable = null;
							componentFocusManager.component.shape_moveable = new dojox.gfx.Moveable(componentFocusManager.component.group);
							console.debug("up");
						}
					});
					
					this.component.tl_res.getNode().style.cursor = "nw-resize";
					this.component.tr_res.getNode().style.cursor = "ne-resize";
					this.component.bl_res.getNode().style.cursor = "sw-resize";
					this.component.br_res.getNode().style.cursor = "se-resize";
				}catch(e){}
			}
			this._publishProperties();
		}else if(this.component != component && component!=null && component!=undefined){
			this.unfocus();
			
			//reassign focus
			this.component = component;
			if(this.component.boundingBox != null){
				this.component.boundingBox.setStroke("black");
			}
			if(this.component.tl_res != null){
				try{
					this.component.resize_shape_group.moveToFront();
					this.component.tl_res.setFill("green").moveToFront();
					this.component.tr_res.setFill("green").moveToFront();
					this.component.bl_res.setFill("green").moveToFront();
					this.component.br_res.setFill("green").moveToFront();
					
					this.component.tl_res.getNode().style.cursor = "nw-resize";
					this.component.tr_res.getNode().style.cursor = "ne-resize";
					this.component.bl_res.getNode().style.cursor = "sw-resize";
					this.component.br_res.getNode().style.cursor = "se-resize";
				}catch(e){}
			}
			this._publishProperties();
		}
	},
	unfocus:function(){
		if(this.component!=null){
			if(this.component.boundingBox != null){
				this.component.boundingBox.setStroke("white");
			}
			if(this.component.tl_res != null){
				try{
					this.component.resize_shape_group.moveToBack();
					this.component.tl_res.setFill("white").moveToBack();
					this.component.tr_res.setFill("white").moveToBack();
					this.component.bl_res.setFill("white").moveToBack();
					this.component.br_res.setFill("white").moveToBack();
				}catch(e){}
			}
			this.component=null;
		}
	},
	moveToFront:function(){
		if(this.component!=null){
			this.component.group.moveToFront();
			sortOrderManager.repairSortOrder(this.component.surface, this.component, "front");
		}
	},
	moveToBack:function(){
		if(this.component!=null){
			this.component.group.moveToBack();
			sortOrderManager.repairSortOrder(this.component.surface, this.component, "back");
		}
	},
	_publishProperties:function(item){
		if(item!=null){
			var attr = dataStore.getAttributes(item);
			var val = [];
			for(var i=0;i<attr.length;i++){
				val[i] = dataStore.getValue(item, attr[i], "");
			}
			dojo.publish("uwsProperties",[{
				attributes:attr,
				values:val
			}]);
		}else if(this.component!=null){
			dataStore.fetchItemByIdentity({
				identity: this.component.id,
				scope: componentFocusManager,
				onItem:function(component){
					var attr = dataStore.getAttributes(component);
					var val = [];
					for(var i=0;i<attr.length;i++){
						val[i] = dataStore.getValue(component, attr[i], "");
					}
					dojo.publish("uwsProperties",[{
						attributes:attr,
						values:val
					}]);
				},
				onError:function(error){
					console.debug(error);
				}
			});
		}
	}
});